package com.deyuanyun.pic.integalz.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.deyuanyun.pic.business.controller.dto.PipeLineNewTaskVO;
import com.deyuanyun.pic.business.service.MergeTaskService;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.ctrl.controller.dto.exinsp.ExinspCipsDcvgDTO;
import com.deyuanyun.pic.ctrl.controller.dto.exinsp.ExinspPcmAcvgDTO;
import com.deyuanyun.pic.ctrl.domain.ExinspTask;
import com.deyuanyun.pic.ctrl.service.EQPTBaseService;
import com.deyuanyun.pic.ctrl.service.ExternalInspProjectService;
import com.deyuanyun.pic.integalz.controller.dto.ExinspAcMiorRecordVO;
import com.deyuanyun.pic.integalz.controller.dto.ExinspCptestStationDTO;
import com.deyuanyun.pic.integalz.controller.dto.ExinspDcMiorRecordVO;
import com.deyuanyun.pic.integalz.controller.dto.ExinspOcgradeVO;
import com.deyuanyun.pic.integalz.controller.dto.IntegalzQueryVO;
import com.deyuanyun.pic.integalz.service.PipeLineExternalTaskService;
import com.deyuanyun.pic.settings.controller.BaseController;

/**
 *
 * @author
 * @date 2016-11-09
 * 完整性分析-外检相关统计
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/integalz")
public class ExternalRelatedCountController extends BaseController{

    @Resource
    private EQPTBaseService eQPTBaseService;
    @Resource
    private PipeLineExternalTaskService pipeLineExternalTaskService;
    @Resource
    private MergeTaskService externalTaskMgrService;
    
    private @Resource ExternalInspProjectService externalInspProjectService;

    /**
     * 跳转到外腐蚀因素分析
     */
    @RequestMapping("/goToOutsideCorrode.html")
    public ModelAndView goToOutsideCorrode(){

        return new ModelAndView("/integalz/outsideCorrode");
    }

    /**
     * 内外检
     * @return
     */
    @RequestMapping("/initInspExinspTask.html")
    public ModelAndView initInspExinspTask(){

        return new ModelAndView("integalz/insideAlign");
    }

    /**
     * 外外检
     * @return
     */
    @RequestMapping("/initExinspExinspTask.html")
    public ModelAndView initExinspExinspTask(){

        return new ModelAndView("integalz/outsideAlign");
    }

    /**
     * 统计管道图层等级
     * @param
     * @return
     */
    @RequestMapping("/queryExinspOcgradeCount.json")
    @ResponseBody
    public void queryExinspOcgradeCount(IntegalzQueryVO query){
        //得到管道上每一段最新的任务
        List<PipeLineNewTaskVO> exinspTaskList = externalInspProjectService.queryNewExternalTaskMerge(query.getPipeLineId(),query.getExinspTaskStartDate(),query.getExinspTaskEndDate());
        // 统计指定距离中最新外检任务对应的管道图层分级评价
        List list = pipeLineExternalTaskService.queryExinspOcgradeCount(query,exinspTaskList);
        List<Object[]> list_t = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            ExinspOcgradeVO exinspOcgradeVO = (ExinspOcgradeVO) list.get(i);

            Object[] obj = new Object[3];
            obj[0] = exinspOcgradeVO.getBeginMileage().longValue() < query.getStartMileage2() ?query.getStartMileage(): exinspOcgradeVO.getBeginMileage().movePointLeft(3);
            obj[1] = exinspOcgradeVO.getEndMileage().longValue() > query.getEndMileage2()?query.getEndMileage():exinspOcgradeVO.getEndMileage().movePointLeft(3);
            obj[2] = exinspOcgradeVO.getCoatingGrade();
            list_t.add(obj);

        }
        list = list_t;
        AjaxSupport.sendSuccessText("",list);
   }

    /**
     * 统计管道漏损点分布
     * @param
     * @return
     */
    @RequestMapping("/queryExinspOclpointCount.json")
    @ResponseBody
    public void queryExinspOclpointCount(IntegalzQueryVO query){
        //得到管道上每一段最新的任务
        List<PipeLineNewTaskVO> exinspTaskList = externalInspProjectService.queryNewExternalTaskMerge(query.getPipeLineId(),query.getExinspTaskStartDate(),query.getExinspTaskEndDate());
        //统计指定距离中最新外检任务对应的漏损点分布
        List list = pipeLineExternalTaskService.queryExinspOclpointCount(query,exinspTaskList);
        AjaxSupport.sendSuccessText("",list);
    }

    /**
     * 统计测试桩检测数据自然电位
     * @param
     * @return
     */
    @RequestMapping("/queryExinspCptestStationCount.json")
    @ResponseBody
    public void queryExinspCptestStationCount(IntegalzQueryVO query){
        //得到管道上每一段最新的任务
        //com.deyuanyun.pic.common.spring.SpringAppliction.getBean("externalInspProjectService")
        List<PipeLineNewTaskVO> exinspTaskList = externalInspProjectService.queryNewExternalTaskMerge(query.getPipeLineId(),query.getExinspTaskStartDate(),query.getExinspTaskEndDate());
        //统计指定距离中最新外检任务对应的测试桩检测数据信息
        List<ExinspCptestStationDTO> list = pipeLineExternalTaskService.queryExinspCptestStationCount(query,exinspTaskList);

        AjaxSupport.sendSuccessText("",list);
    }

    /**
     * 交直流干扰
     * @param
     * @return
     */
    @RequestMapping("/queryStrayCurrentCount.json")
    @ResponseBody
    public void queryStrayCurrentCount(IntegalzQueryVO query){
        //得到管道上每一段最新的任务
        List<PipeLineNewTaskVO> exinspTaskList = externalInspProjectService.queryNewExternalTaskMerge(query.getPipeLineId(),query.getExinspTaskStartDate(),query.getExinspTaskEndDate());
        /**
         * 交流干扰监测记录
         */
        List<ExinspAcMiorRecordVO> acList = pipeLineExternalTaskService.queryExinspAcMiorRecord(query,exinspTaskList);
        /**
         * 直流干扰监测记录
         */
        List<ExinspDcMiorRecordVO> dcList =pipeLineExternalTaskService.queryExinspDcMiorRecord(query,exinspTaskList);

        List list = new ArrayList();
        list.addAll(acList);
        list.addAll(list.size(),dcList);
        AjaxSupport.sendSuccessText("",list);
    }

    /**
     * 获取外检PcmAcvg数据
     * 
     * @param query
     */
    @RequestMapping("/queryExinspPcmAcvg.json")
    @ResponseBody
    public void queryExinspPcmAcvg(IntegalzQueryVO query){

        String pipeLineId = query.getPipeLineId();
        List<PipeLineNewTaskVO> exinspTaskList = externalInspProjectService.queryNewExternalTaskMerge(pipeLineId,query.getExinspTaskStartDate(),query.getExinspTaskEndDate());
        
        List<ExinspPcmAcvgDTO> list = pipeLineExternalTaskService.queryExinspPcmAcvg(query, exinspTaskList);
        AjaxSupport.sendSuccessText("",list);
    }
    
    //目的是为了针对不同的东西，过滤掉null值。
    private void fillCipsDcvgMap(Map<String,Object> model,List<ExinspCipsDcvgDTO> list){
        List<ExinspCipsDcvgDTO> list1 = new ArrayList<ExinspCipsDcvgDTO>();
        List<ExinspCipsDcvgDTO> list2 = new ArrayList<ExinspCipsDcvgDTO>();
        List<ExinspCipsDcvgDTO> list3 = new ArrayList<ExinspCipsDcvgDTO>();
        for (ExinspCipsDcvgDTO exinspCipsDcvg : list) {
            
            Integer cipsOnPotential = exinspCipsDcvg.getCipsOnPotential();
            if(cipsOnPotential != null){
                list1.add(exinspCipsDcvg);
            }
            Integer cipsOffPotential = exinspCipsDcvg.getCipsOffPotential();
            if(cipsOffPotential != null){
                list2.add(exinspCipsDcvg);
            }
            BigDecimal dcvgIr = exinspCipsDcvg.getDcvgIr();
            if(dcvgIr != null){
                list3.add(exinspCipsDcvg);
            }
        }
        
        model.put("cipsOnPotentialList", list1);
        model.put("cipsOffPotentialList", list2);
        model.put("dcvgIrList", list3);
    }

    /**
     * 获取CipsDcvg
     * 
     * @param query
     */
    @RequestMapping("/queryExinspCipsDvcg.json")
    @ResponseBody
    public void queryExinspCipsDvcg(IntegalzQueryVO query,ModelMap model){

        String pipeLineId = query.getPipeLineId();
        List<PipeLineNewTaskVO> exinspTaskList = externalInspProjectService.queryNewExternalTaskMerge(pipeLineId,query.getExinspTaskStartDate(),query.getExinspTaskEndDate());
        
        List<ExinspCipsDcvgDTO> list = pipeLineExternalTaskService.queryExinspCipsDcvg(query, exinspTaskList);
        //cipsOnPotential
        //cipsOffPotential
        //dcvgIr
        fillCipsDcvgMap(model,list);
        
        AjaxSupport.sendSuccessText("",model);
    }
    

    /**
     * 获取两个外检任务的pcm、acvg
     * @param query
     * @param exinspTaskId1
     * @param exinspTaskId2
     * @param model
     */
    @RequestMapping("/queryExinspPcmAcvg_2Task.json")
    @ResponseBody
    public void queryExinspPcmAcvg_2Task(IntegalzQueryVO query
            ,@RequestParam("exinspTaskId1") String exinspTaskId1
            ,@RequestParam("exinspTaskId2") String exinspTaskId2
            ,ModelMap model){

        List<PipeLineNewTaskVO> exinspTaskList1 = new ArrayList<PipeLineNewTaskVO>(1);
        exinspTaskList1.add(toPipeLineNewTaskVO(query,exinspTaskId1));
        List<ExinspPcmAcvgDTO> list1 = pipeLineExternalTaskService.queryExinspPcmAcvg(query, exinspTaskList1);
        model.addAttribute("exinspTask1", list1);

        List<PipeLineNewTaskVO> exinspTaskList2 = new ArrayList<PipeLineNewTaskVO>(1);
        exinspTaskList2.add(toPipeLineNewTaskVO(query,exinspTaskId2));
        List<ExinspPcmAcvgDTO> list2 = pipeLineExternalTaskService.queryExinspPcmAcvg(query, exinspTaskList2);
        model.addAttribute("exinspTask2", list2);


        AjaxSupport.sendSuccessText("",model);
    }

    /**
     * 获取两个任务的CipsDcvg
     * 
     * @param query
     * @param exinspTaskId1
     * @param exinspTaskId2
     * @param model
     */
    @RequestMapping("/queryExinspCipsDcvg_2Task.json")
    @ResponseBody
    public void queryExinspCipsDcvg_2Task(IntegalzQueryVO query
            ,@RequestParam("exinspTaskId1") String exinspTaskId1
            ,@RequestParam("exinspTaskId2") String exinspTaskId2
            ,ModelMap model){

        List<PipeLineNewTaskVO> exinspTaskList1 = new ArrayList<PipeLineNewTaskVO>(1);
        exinspTaskList1.add(toPipeLineNewTaskVO(query,exinspTaskId1));
        List<ExinspCipsDcvgDTO> list1 = pipeLineExternalTaskService.queryExinspCipsDcvg(query, exinspTaskList1);
        Map<String,Object> exinspTask1Map = new HashMap<String, Object>();
        fillCipsDcvgMap(exinspTask1Map,list1);
        model.addAttribute("exinspTask1", exinspTask1Map);

        List<PipeLineNewTaskVO> exinspTaskList2 = new ArrayList<PipeLineNewTaskVO>(1);
        exinspTaskList2.add(toPipeLineNewTaskVO(query,exinspTaskId2));
        List<ExinspCipsDcvgDTO> list2 = pipeLineExternalTaskService.queryExinspCipsDcvg(query, exinspTaskList2);
        Map<String,Object> exinspTask1Map2 = new HashMap<String, Object>();
        fillCipsDcvgMap(exinspTask1Map2,list2);
        model.addAttribute("exinspTask2", exinspTask1Map2);


        AjaxSupport.sendSuccessText("",model);
    }


    /**
     * 统计测试桩检测数据自然电位
     * @param
     * @return
     */
    @RequestMapping("/queryExinspCptestStation_2Task.json")
    @ResponseBody
    public void queryExinspCptestStation_2Task(IntegalzQueryVO query
            ,@RequestParam("exinspTaskId1") String exinspTaskId1
            ,@RequestParam("exinspTaskId2") String exinspTaskId2
            ,ModelMap model){
        //得到管道上每一段最新的任务

        List<PipeLineNewTaskVO> exinspTaskList1 = new ArrayList<PipeLineNewTaskVO>(1);
        exinspTaskList1.add(toPipeLineNewTaskVO(query,exinspTaskId1));
        List<ExinspCptestStationDTO> list1 = pipeLineExternalTaskService.queryExinspCptestStationCount(query, exinspTaskList1);
        model.addAttribute("exinspTask1", list1);

        List<PipeLineNewTaskVO> exinspTaskList2 = new ArrayList<PipeLineNewTaskVO>(1);
        exinspTaskList2.add(toPipeLineNewTaskVO(query,exinspTaskId2));
        List<ExinspCptestStationDTO> list2 = pipeLineExternalTaskService.queryExinspCptestStationCount(query, exinspTaskList2);
        model.addAttribute("exinspTask2", list2);

        AjaxSupport.sendSuccessText("",model);
    }

    
    
    public PipeLineNewTaskVO toPipeLineNewTaskVO(IntegalzQueryVO query,String exinspTaskId){
        PipeLineNewTaskVO pipeLineNewTaskVO1 = new PipeLineNewTaskVO();
        pipeLineNewTaskVO1.setStartMileage(query.getStartMileage());
        pipeLineNewTaskVO1.setEndMileage(query.getEndMileage());
        pipeLineNewTaskVO1.setIncludeStart(true);
        pipeLineNewTaskVO1.setIncludeEnd(true);
        pipeLineNewTaskVO1.setId(exinspTaskId);
        return pipeLineNewTaskVO1;
    }
    
    
    @RequestMapping("/queryExinspTask.json")
    @ResponseBody
    public void queryExinspTask(IntegalzQueryVO query,@RequestParam(required = false,value = "excludeExinspTaskId") String excludeExinspTaskId){

        String pipeLineId = query.getPipeLineId();
        Long start = query.getStartMileage2();
        Long end = query.getEndMileage2();
        List<ExinspTask> exinspTaskList = externalInspProjectService.queryExinspTask(pipeLineId, start, end);
        if(StringUtils.isNotEmpty(excludeExinspTaskId)){
            for (int i = 0; i < exinspTaskList.size(); i++) {
                ExinspTask exinspTask = exinspTaskList.get(i);
                if(excludeExinspTaskId.equals(exinspTask.getId())){
                    exinspTaskList.remove(i--);
                }
            }   
        }
        
        
        AjaxSupport.sendSuccessText("",exinspTaskList);
    }

    

}
